﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public class GestionMails
{
    public bool GrabarMail(String a, String b, String c, String d)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        Mails mail = new Mails(a, b, c, d);
        if (cn != null)
        {
            SqlCommand cmd;
            String sql = "INSERT into Bandeja_Mails values(";
            sql += "'" + mail.Remitente + "',";
            sql += "'" + mail.Destinatario + "',";
            sql += "'" + mail.Asunto + "',";
            sql += "'" + mail.TextoMail + "',";
            sql += "'" + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToShortTimeString().ToString() + "',";
            sql += "'0',";
            sql += "'False',";
            sql += "'1')";
            cmd = new SqlCommand(sql, cn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return false;
    }

    public bool GrabarBorrador(Borrador mail)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        if (cn != null)
        {
            SqlCommand cmd;
            String sql = "INSERT into Bandeja_Mails values(";
            sql += "'" + mail.Remitente + "',";
            sql += "'" + mail.Destinatario + "',";
            sql += "'" + mail.Asunto + "',";
            sql += "'" + mail.TextoMail + "',";
            sql += "'null',";
            sql += "'1',";
            sql += "'False',";
            sql += "'1')";
            cmd = new SqlCommand(sql, cn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return false;
    }

    public void CargarBandeja(String consulta,ref GridView grdMails)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;
        SqlDataReader dr;
        String sql = consulta;
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                dr = cmd.ExecuteReader();
                grdMails.DataSource = dr;
                grdMails.DataBind();

            }
            catch (SqlException)
            {
                return;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return;
    }

    public void DestacarMail(String consulta)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;

        String sql = consulta;
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                cmd.ExecuteNonQuery();

            }
            catch (SqlException)
            {
                return;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return;
    }

    public void BorrarMail(String consulta)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;

        String sql = consulta;
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                cmd.ExecuteNonQuery();

            }
            catch (SqlException)
            {
                return;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return;
    }
}